/*
 * Copyright 2020-2030 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package com.gitee.minimalismstyle.fresh.simple.service.impl

import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.extra.copyProperties
import com.gitee.minimalismstyle.fresh.common.data.beetl.service.impl.LongModelBaseServiceImpl
import com.gitee.minimalismstyle.fresh.simple.dao.SimpleActionDao
import com.gitee.minimalismstyle.fresh.simple.api.model.SimpleAction
import com.gitee.minimalismstyle.fresh.simple.api.model.SimpleActionFields
import com.gitee.minimalismstyle.fresh.simple.api.service.SimpleActionService
import com.gitee.minimalismstyle.fresh.simple.dao.SimpleActionFieldDao
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Primary
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 事件组业务实现
 * @author maoxiaodong
*/
@Primary
@Service
class SimpleActionServiceImpl: SimpleActionService, LongModelBaseServiceImpl<SimpleActionDao, SimpleAction>() {
    @Autowired
    private lateinit var simpleActionFieldDao: SimpleActionFieldDao

    @Transactional
    override fun saveSimpleActionFields(simpleActionFields: SimpleActionFields): R<SimpleActionFields> {
        if(simpleActionFields.id != null){
            simpleActionFieldDao.deleteByActionId(simpleActionFields.id!!)
        }
        val simpleAction = simpleActionFields.copyProperties(SimpleAction::class)
        dao.save(simpleAction)
        simpleAction.copyProperties(simpleActionFields)
        if(simpleActionFields.fields.isNotEmpty()){
            simpleActionFields.fields.forEach {
                it.reset()
                it.actionId = simpleActionFields.id
            }
            simpleActionFieldDao.insertAll(simpleActionFields.fields)
        }
        return R.success(simpleActionFields)
    }

    override fun findSimpleActionFieldsById(id: Long): R<SimpleActionFields> {
        val simpleActionFields = SimpleActionFields()
        val simpleAction = dao.findById(id)
        BeanUtils.copyProperties(simpleAction, simpleActionFields)
        simpleActionFields.fields = simpleActionFieldDao.findByActionId(id)
        return R.success(simpleActionFields)
    }

    override fun findSimpleActionFieldsByCode(code: String): R<SimpleActionFields> {
        val simpleActionFields = SimpleActionFields()
        val simpleAction = dao.findByCode(code)
        BeanUtils.copyProperties(simpleAction, simpleActionFields)
        simpleActionFields.fields = simpleActionFieldDao.findByActionId(simpleAction.id!!)
        return R.success(simpleActionFields)
    }

}